package com.abel.quiz.controller;/**
 * Created by abel.lin on 2017/8/4.
 */


import com.abel.quiz.common.MyDateEditor;
import com.abel.quiz.vo.UserInfoDomain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.util.Date;

/**
 * @author abel.lin
 * @date_time 2017/8/4 13:09
 * @description
 */
public class BaseController {
    public Logger log = LoggerFactory.getLogger(getClass());

    @InitBinder
    public void initDate(WebDataBinder wdb){
        wdb.registerCustomEditor(Date.class, new MyDateEditor());
    }

    public UserInfoDomain getCurrentUser(){
        Object principal = SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        if(principal instanceof  UserInfoDomain){
            UserInfoDomain userDomain = (UserInfoDomain)principal;
            return userDomain;
        }
        return null;
    }

    public String getCurrentUserName(){
        String userName = "";
        UserInfoDomain user = getCurrentUser();
        if(user != null){
            userName = user.getUsername();
        }
        return userName;
    }
}
